﻿#ifndef XRTCCLIENT_MEDIA_SOURCE_VCM_CAPTURER_H_
#define XRTCCLIENT_MEDIA_SOURCE_VCM_CAPTURER_H_

#include <string>

#include "xrtc/ixrtc_media_source.h"

#include <rtc_base/thread.h>
#include <api/media_stream_interface.h>
#include <modules/video_capture/video_capture.h>
#include <media/base/video_broadcaster.h>

namespace xrtc {

class VcmCapturer : public IXRTCVideoSource,
                    public rtc::VideoSinkInterface<webrtc::VideoFrame>,
                    public rtc::VideoSourceInterface<webrtc::VideoFrame>
{
public:
    static VcmCapturer* Create(size_t width, size_t height, size_t fps,
        const std::string& device_id);

    ~VcmCapturer() override;

    bool Start() override;
    void Stop() override;

    // rtc::VideoSinkInterface
    void OnFrame(const webrtc::VideoFrame& frame) override;

    // rtc::VideoSourceInterface
    void AddOrUpdateSink(VideoSinkInterface<webrtc::VideoFrame>* sink,
        const rtc::VideoSinkWants& wants) override;
    // RemoveSink must guarantee that at the time the method returns,
    // there is no current and no future calls to VideoSinkInterface::OnFrame.
    void RemoveSink(VideoSinkInterface<webrtc::VideoFrame>* sink) override;

private:
    VcmCapturer(size_t width, size_t height, size_t fps,
        const std::string& device_id);

    bool Init(size_t width, size_t height, size_t fps,
        const std::string& device_id);
    void Destroy();

private:
    rtc::Thread* current_thread_;
    size_t width_;
    size_t height_;
    size_t fps_;
    std::string device_id_;
    rtc::scoped_refptr<webrtc::VideoCaptureModule> vcm_;
    webrtc::VideoCaptureCapability capability_;
    rtc::VideoBroadcaster broadcaster_;
};

} // namespace xrtc

#endif // XRTCCLIENT_MEDIA_SOURCE_VCM_CAPTURER_H_